var Log = require("./core_log.js");
var Tool = require("./tool.js");

/**
 * 微信发起申请支付
 */
function wxRequestPayment(data, success, fail) {
  var logCallerName = wxRequestPayment.name;
  /**
      * 文档：
      * https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html
      * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=5
      */
  wx.requestPayment({
    timeStamp: data.timeStamp,
    nonceStr: data.nonceStr,
    package: data.package,
    signType: data.signType,
    paySign: data.paySign,
    success(res) {
      const errMsg = res.errMsg;
      if (errMsg == "requestPayment:ok") {
        Log.debug(logCallerName, '支付成功', res);
        success(res);
      } else {
        Log.debug(logCallerName, '支付失败', res);
        fail ? fail(res) : Tool.showInfo("支付失败");
      }
    },
    fail(res) {
      Log.debug(logCallerName, '支付失败', res);
      // if (res.errMsg == "requestPayment:fail cancel") { 
      fail ? fail(res) : Tool.showInfo("支付失败");
      // }
    }
  })
}

/**
 * exports
 */
module.exports = {
  wxRequestPayment: wxRequestPayment
};